package com.example.controller.pay;

import com.alibaba.fastjson.JSONObject;
import com.example.config.auth.SecurityUtil;
import com.example.config.exception.GlobalExcetion;
import com.example.ribbon.service.PayAccountRibbonService;
import com.example.util.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/pay")
public class PayAccountController {

    @Autowired
    private PayAccountRibbonService payAccountRibbonService;

    @GetMapping("/info")
    public JSONObject info() {
        String userName= SecurityUtil.getCurrentUsername();
        return payAccountRibbonService.getUserMoneyAccountInfo(userName);
    }

    /**
     * 获取进账
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getPayRecordIn")
    public JSONObject getPayRecordIn(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                     @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize) {
        String userId=SecurityUtil.getUserId();
        return payAccountRibbonService.getPayRecordInPage(userId,pageNum,pageSize);
    }

    /**
     * 获取出账记录
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getPayRecordOut")
    public JSONObject getPayRecordOut(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                     @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize) {
        String userId=SecurityUtil.getUserId();
        return payAccountRibbonService.getPayRecordOutPage(userId,pageNum,pageSize);
    }


    @PostMapping("/transfer")
    public JSONObject transfer(@RequestBody JSONObject object) throws GlobalExcetion {
        String userId=SecurityUtil.getUserId();
        if (!object.containsKey("userName")){
            throw new GlobalExcetion(1,"转帐账号不得为空");
        }
        if(!object.containsKey("payPassword")){
            throw new GlobalExcetion(1,"支付密码不得为空");
        }
        if(!object.containsKey("payMoney")){
            throw new GlobalExcetion(1,"支付金额不得为空");
        }
        if(!object.containsKey("payNote")){
            throw new GlobalExcetion(1,"支付备注不得为空");
        }
        return payAccountRibbonService.transfer(userId,
                object.getString("userName"),
                object.getString("payPassword"),
                object.getString("payMoney"),
                object.getString("payNote")
                );
    }

    @PostMapping("insvertMe")
    public JSONObject insvertMoney(@RequestParam("money")String money){
        String username=SecurityUtil.getCurrentUsername();
        return payAccountRibbonService.insertMoney(username,money);
    }

    @PostMapping("/insvet")
    public JSONObject insvertMoney(@RequestBody JSONObject jsonObject){
        if(!jsonObject.containsKey("userName")){
            return Results.error("用户账号不得为空");
        }
        if(!jsonObject.containsKey("money")){
            return Results.error("金额不得为空");
        }
        return payAccountRibbonService.insertMoney(jsonObject.getString("userName"),jsonObject.getString("money"));
    }
}
